//
// Copyright (c) 2009-2011, chunquedong
//
// This file is part of ChunMap project
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
//
// History:
//   2011-10-29  Jed Young  Creation
//

using fwt
using gfx
using array

using chunmapModel

class AreaTool : LineTool
{
  protected override Void finished(CoordSeqBuf lse)
  {
    if (onCreateFinish != null)
    {
      lse.close
      Ring? ls
      try
        ls = Ring(lse.toCoordSeq)
      catch
        return
      onCreateFinish?.call(Polygon(ls))
    }
  }

  protected override Array toIntArray(Int x, Int y)
  {
    ps := Array.allocate((lse.size+2) * 2)
    n := lse.size
    i:=0
    for (; i<n; ++i)
    {
      p := lse.get(i)
      ps.setInt(i*2, map.view.x2Screen(p.x).toInt)
      ps.setInt(i*2+1, map.view.y2Screen(p.y).toInt)
    }
    k := n*2
    ps.setInt(k, x)
    ps.setInt(k+1, y)
    first := lse.first
    ps.setInt(k+2, map.view.x2Screen(first.x).toInt)
    ps.setInt(k+3, map.view.y2Screen(first.y).toInt)
    return ps
  }
}